<template>
  <div class="campus-map">
    <div class="map-title">🏫 学校地图（楼宇情况）</div>
    <div class="map-container">
      <!-- 地图网格背景 -->
      <div class="map-grid">
        <div v-for="i in 20" :key="`h-${i}`" class="grid-line horizontal" :style="{ top: `${i * 5}%` }"></div>
        <div v-for="i in 20" :key="`v-${i}`" class="grid-line vertical" :style="{ left: `${i * 5}%` }"></div>
      </div>
      
      <!-- 建筑物 -->
      <div class="buildings-layer">
        <div 
          v-for="building in buildingList" 
          :key="building.id"
          class="building-item"
          :class="building.status"
          :style="getBuildingStyle(building)"
          @click="handleBuildingClick(building)"
        >
          <div class="building-icon">🏢</div>
          <div class="building-name">{{ building.name }}</div>
          <div class="building-status" :class="building.status">
            {{ getStatusText(building.status) }}
          </div>
          <div class="building-devices">{{ building.deviceCount }}台</div>
        </div>
      </div>
      
      <!-- 连接线 -->
      <div class="connections-layer">
        <svg class="connection-svg" viewBox="0 0 100 100">
          <defs>
            <pattern id="flowPattern" patternUnits="userSpaceOnUse" width="10" height="10">
              <circle cx="5" cy="5" r="1" fill="rgba(0, 212, 255, 0.6)">
                <animate attributeName="opacity" values="0;1;0" dur="2s" repeatCount="indefinite"/>
              </circle>
            </pattern>
          </defs>
          <!-- 连接线 -->
          <path d="M 20 30 Q 50 20 80 30" stroke="url(#flowPattern)" stroke-width="1" fill="none" opacity="0.7"/>
          <path d="M 20 50 Q 50 40 80 50" stroke="url(#flowPattern)" stroke-width="1" fill="none" opacity="0.7"/>
          <path d="M 20 70 Q 50 60 80 70" stroke="url(#flowPattern)" stroke-width="1" fill="none" opacity="0.7"/>
        </svg>
      </div>
      
      <!-- 状态指示器 -->
      <div class="status-indicators">
        <div class="indicator online">
          <div class="indicator-dot"></div>
          <span>在线</span>
        </div>
        <div class="indicator maintenance">
          <div class="indicator-dot"></div>
          <span>维护</span>
        </div>
        <div class="indicator offline">
          <div class="indicator-dot"></div>
          <span>离线</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { BuildingStatus } from '../types'

interface Props {
  buildingList: BuildingStatus[]
}

interface Emits {
  (e: 'buildingClick', building: BuildingStatus): void
}

const { buildingList } = defineProps<Props>()
const emit = defineEmits<Emits>()

const handleBuildingClick = (building: BuildingStatus) => {
  emit('buildingClick', building)
}

const getStatusText = (status: BuildingStatus['status']) => {
  const statusMap = {
    'online': '在线',
    'maintenance': '维护',
    'offline': '离线'
  }
  return statusMap[status] || '未知'
}

// 根据建筑索引获取位置样式
const getBuildingStyle = (building: BuildingStatus) => {
  const positions = [
    { left: '15%', top: '25%' },  // 教学楼A
    { left: '85%', top: '25%' },  // 教学楼B
    { left: '15%', top: '50%' },  // 图书馆
    { left: '85%', top: '50%' },  // 实验楼
    { left: '15%', top: '75%' },  // 办公楼
    { left: '85%', top: '75%' }   // 食堂
  ]
  
  const index = parseInt(building.id) - 1
  const position = positions[index] || { left: '50%', top: '50%' }
  
  return {
    left: position.left,
    top: position.top
  }
}
</script>

<style scoped>
.campus-map {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(0, 212, 255, 0.3);
  border-radius: 12px;
  padding: 15px;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
}

.map-title {
  color: #00d4ff;
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 10;
}

.map-container {
  flex: 1;
  position: relative;
  background: linear-gradient(135deg, rgba(0, 20, 40, 0.8), rgba(0, 40, 80, 0.6));
  border-radius: 8px;
  overflow: hidden;
}

/* 网格背景 */
.map-grid {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  opacity: 0.3;
}

.grid-line {
  position: absolute;
  background: rgba(0, 212, 255, 0.2);
}

.grid-line.horizontal {
  width: 100%;
  height: 1px;
  left: 0;
}

.grid-line.vertical {
  width: 1px;
  height: 100%;
  top: 0;
}

/* 建筑物层 */
.buildings-layer {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.building-item {
  position: absolute;
  width: 80px;
  height: 80px;
  background: rgba(0, 212, 255, 0.1);
  border: 2px solid rgba(0, 212, 255, 0.5);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  transform: translate(-50%, -50%);
}

.building-item:hover {
  transform: translate(-50%, -50%) scale(1.1);
  box-shadow: 0 0 20px rgba(0, 212, 255, 0.6);
  z-index: 10;
}

.building-item.online {
  border-color: rgba(103, 224, 227, 0.7);
  background: rgba(103, 224, 227, 0.1);
}

.building-item.maintenance {
  border-color: rgba(255, 217, 61, 0.7);
  background: rgba(255, 217, 61, 0.1);
}

.building-item.offline {
  border-color: rgba(128, 128, 128, 0.7);
  background: rgba(128, 128, 128, 0.1);
}

.building-icon {
  font-size: 20px;
  margin-bottom: 2px;
}

.building-name {
  color: #fff;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  line-height: 1;
  margin-bottom: 2px;
}

.building-status {
  font-size: 8px;
  padding: 1px 4px;
  border-radius: 2px;
  margin-bottom: 1px;
}

.building-status.online {
  background: rgba(103, 224, 227, 0.3);
  color: #67e0e3;
}

.building-status.maintenance {
  background: rgba(255, 217, 61, 0.3);
  color: #ffd93d;
}

.building-status.offline {
  background: rgba(128, 128, 128, 0.3);
  color: #888;
}

.building-devices {
  font-size: 8px;
  color: rgba(255, 255, 255, 0.7);
}

/* 连接线层 */
.connections-layer {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
}

.connection-svg {
  width: 100%;
  height: 100%;
}

/* 状态指示器 */
.status-indicators {
  position: absolute;
  bottom: 10px;
  left: 10px;
  display: flex;
  gap: 15px;
}

.indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.8);
}

.indicator-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.indicator.online .indicator-dot {
  background: #67e0e3;
}

.indicator.maintenance .indicator-dot {
  background: #ffd93d;
}

.indicator.offline .indicator-dot {
  background: #888;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.5; }
  100% { opacity: 1; }
}
</style>
